<?php

include("../includes/common.php");

$title = '数据迁移';
include './head.php';
if ($islogin == 1) {
} else exit("<script language='javascript'>window.location.href='./login.php';</script>");
?>
<div class="col-sm-12 col-md-10 center-block" style="float: none;">
    <?php
    $my = isset($_GET['my']) ? $_GET['my'] : null;
    if($my=='add'){

    ?>
        <div class="block">
            <div class="block-title">
                <h3 class="panel-title">数据迁移</h3>
            </div>
            <div class="">
                <form action="./datamove.php?my=add_submit" method="post" role="form">
                    <div class="form-group">
                        <label>选择迁移系统</label><br />
                        <select class="form-control" name="type">
                            <!--<option value="1">祥云系统</option>-->
                            <option value="2">沉梦系统</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label>选择可迁移的数据</label><span id="btn1" class="pull-right btn btn-xs btn-primary">全选</span><br />
                        <label class="checkbox-inline"><input type="checkbox" name="permission[]" value="pay"> 支付记录</label>
                        <label class="checkbox-inline"><input type="checkbox" name="permission[]" value="order"> 订单列表</label>
                        <label class="checkbox-inline"><input type="checkbox" name="permission[]" value="class"> 分类列表</label>
                        <label class="checkbox-inline"><input type="checkbox" name="permission[]" value="tools"> 商品列表</label>
                        <label class="checkbox-inline"><input type="checkbox" name="permission[]" value="shequ"> 社区对接列表</label>
                        <label class="checkbox-inline"><input type="checkbox" name="permission[]" value="price"> 加价模板</label>
                        <label class="checkbox-inline"><input type="checkbox" name="permission[]" value="faka"> 发卡库存</label>
                        <label class="checkbox-inline"><input type="checkbox" name="permission[]" value="site"> 分站/用户</label>
                        <label class="checkbox-inline"><input type="checkbox" name="permission[]" value="workorder"> 工单管理</label>
                        <label class="checkbox-inline"><input type="checkbox" name="permission[]" value="message"> 站内通知</label>
                        <label class="checkbox-inline"><input type="checkbox" name="permission[]" value="article"> 文章管理</label>
                        <label class="checkbox-inline"><input type="checkbox" name="permission[]" value="record"> 收支明细</label>
                        <label class="checkbox-inline"><input type="checkbox" name="permission[]" value="tixian"> 提现记录</label>
                    </div>
                    <div class="form-group">
                        <input type="submit" name="submit" value="添加" class="btn btn-primary btn-block" />
                    </div>
                </form>
            </div>
        </div>
        <script>
        $("#btn1").click(function(){
            $("input[name='permission[]']").attr("checked","true"); 
        })
        </script>

    <?php
}elseif ($my == 'add_submit') {
        $type = $_POST['type']??'1';
        switch ($type) {
            case '2':
                $permission = $_POST['permission'];
                if (in_array('pay', $permission)) {
                    $DB->exec("DROP TABLE IF EXISTS `pre_pay`");
                    $DB->exec("ALTER TABLE `cmy_pay` RENAME TO `pre_pay`");
                    $DB->exec("ALTER TABLE `pre_pay` change siteurl `domain` varchar(64) DEFAULT NULL,");
                    $DB->exec("ALTER TABLE `pre_pay` ADD COLUMN `api_trade_no` varchar(64) DEFAULT NULL");
                    $DB->exec("ALTER TABLE `pre_pay` ADD COLUMN `channel` varchar(10) NULL");
                    $DB->exec("ALTER TABLE `pre_pay` ADD COLUMN `blockdj` tinyint(1) NOT NULL DEFAULT '0'");
                }
                if (in_array('order', $permission)) {
                    $DB->exec("DROP TABLE IF EXISTS `pre_orders`");
                    $DB->exec("ALTER TABLE `cmy_orders` RENAME TO `pre_orders`");
                    $DB->exec("ALTER TABLE `pre_orders` ADD INDEX input (`input`)");
                    $DB->exec("ALTER TABLE `pre_orders` change payorder tradeno varchar(64) DEFAULT NULL");
                }
                if (in_array('class', $permission)) {
                    $DB->exec("DROP TABLE IF EXISTS `pre_class`");
                    $DB->exec("ALTER TABLE `cmy_class` RENAME TO `pre_class`");
                    $DB->exec("ALTER TABLE `pre_class` change hidepays blockpay VARCHAR(255) DEFAULT NULL");
                }
                if (in_array('tools', $permission)) {
                    $DB->exec("DROP TABLE IF EXISTS `pre_tools`");
                    $DB->exec("ALTER TABLE `cmy_tools` RENAME TO `pre_tools`");
                    $DB->exec("ALTER TABLE `pre_tools` modify column `price` decimal(10,2) DEFAULT '0.00'");
                    $DB->exec("ALTER TABLE `pre_tools` modify column `cost` decimal(10,2) DEFAULT '0.00'");
                    $DB->exec("ALTER TABLE `pre_tools` modify column `cost2` decimal(10,2) DEFAULT '0.00'");
                    $DB->exec("ALTER TABLE `pre_tools` ADD COLUMN `goods_sid` tinyint(1) NOT NULL DEFAULT '0'");
                    $DB->exec("ALTER TABLE `pre_tools` ADD COLUMN `audit_status` tinyint(1) NOT NULL DEFAULT '0'");
                    $DB->exec("ALTER TABLE `pre_tools` ADD COLUMN `sup_price` decimal(10,2) NOT NULL DEFAULT '0.00'");
                    $DB->exec("ALTER TABLE `pre_tools` ADD COLUMN `ts` tinyint(1) DEFAULT '0'");
                    $DB->exec("ALTER TABLE `pre_tools` ADD COLUMN `valiserv` varchar(15) DEFAULT NULL");
                }
                if (in_array('shequ', $permission)) {
                    $DB->exec("DROP TABLE IF EXISTS `pre_shequ`");
                    $DB->exec("ALTER TABLE `cmy_shequ` RENAME TO `pre_shequ`");
                    $DB->exec("ALTER TABLE `pre_shequ` modify column `type` varchar(20) NOT NULL");
                    $DB->exec("ALTER TABLE `pre_shequ` change orderstatus result tinyint(1) NOT NULL DEFAULT '1'");
                    $DB->exec("ALTER TABLE `pre_shequ` ADD COLUMN `remark` varchar(255) DEFAULT NULL");
                    $DB->exec("ALTER TABLE `pre_shequ` ADD COLUMN `protocol` tinyint(1) NOT NULL DEFAULT 0");
                    $DB->exec("ALTER TABLE `pre_shequ` ADD COLUMN `monitor` tinyint(1) NOT NULL DEFAULT 0");
                    $rs = $DB->query("SELECT id,type,url,protocol FROM `pre_shequ` where 1");
                    $config = $DB->getRow("select * from cmy_config where k='syskey' limit 1");
                    while($res = $rs->fetch()){
                        $arr = parse_url($res['url']);
                        $url = $arr['host'];
                        $protocol = $arr['scheme']=='https'?1:0;
                        $password = authcode($res['password'], 'DECODE', $config['syskey']);
                        switch($res['type']){
                            case '0'://九五
                                $DB->exec("update pre_shequ set type='jiuwu',url='$url',protocol='$protocol',password='$password' where id='{$res['id']}' limit 1");
                                break;
                            case '1'://亿乐
                                $DB->exec("update pre_shequ set type='yile',url='$url',protocol='$protocol',password='$password' where id='{$res['id']}' limit 1");
                                break;
                             case '6'://卡易信
                                $DB->exec("update pre_shequ set type='kayixin',url='$url',protocol='$protocol',password='$password' where id='{$res['id']}' limit 1");
                                break;
                             case '9'://卡商网
                                $DB->exec("update pre_shequ set type='kashangwl',url='$url',protocol='$protocol',password='$password' where id='{$res['id']}' limit 1");
                                break;
                            case '12':
                            case '13'://同系统
                                $DB->exec("update pre_shequ set type='daishua',url='$url',protocol='$protocol',password='$password' where id='{$res['id']}' limit 1");
                                break;
                             case '22'://商战网
                                $DB->exec("update pre_shequ set type='shangzhanwl',url='$url',protocol='$protocol',password='$password' where id='{$res['id']}' limit 1");
                                break;
                             case '25'://直客
                                $DB->exec("update pre_shequ set type='zhike',url='$url',protocol='$protocol',password='$password' where id='{$res['id']}' limit 1");
                                break;
                             case '18'://卡卡云
                                $DB->exec("update pre_shequ set type='kakayun',url='$url',protocol='$protocol',password='$password' where id='{$res['id']}' limit 1");
                                break;
                            default:
                                $DB->exec("update pre_shequ set url='$url',protocol='$protocol',password='$password' where id='{$res['id']}' limit 1");
                                break;
                        }
                    }
                }
                if (in_array('price', $permission)) {
                    $DB->exec("DROP TABLE IF EXISTS `pre_price`");
                    $DB->exec("ALTER TABLE `cmy_price` RENAME TO `pre_price`");
                    $rs = $DB->query("SELECT kind FROM `pre_price` where 1");
                    while($res = $rs->fetch()){
                        if($res['kind'] == 2){
                            $DB->exec("update pre_price set kind='1' where id='{$res['id']}' limit 1");
                        }
                    }
                }
                if (in_array('faka', $permission)) {
                    $DB->exec("DROP TABLE IF EXISTS `pre_faka`");
                    $DB->exec("ALTER TABLE `cmy_faka` RENAME TO `pre_faka`");
                    $DB->exec("ALTER TABLE `pre_faka` ADD COLUMN `ts` tinyint(1) DEFAULT '0'");
                    $DB->exec("ALTER TABLE `pre_faka` ADD INDEX tid (`tid`)");
                    $DB->exec("ALTER TABLE `pre_faka` ADD INDEX getleft (`tid`,`orderid`)");
                }
                if (in_array('site',$permission)) {
                    $DB->exec("DROP TABLE IF EXISTS `pre_site`");
                    $DB->exec("ALTER TABLE `cmy_site` RENAME TO `pre_site`");
                    $DB->exec("ALTER TABLE `pre_site` drop column `rmb`");
                    $DB->exec("ALTER TABLE `pre_site` drop column `rmbtc`");
                    $DB->exec("ALTER TABLE `pre_site` drop column `domain`");
                    $DB->exec("ALTER TABLE `pre_site` drop column `domain2`");
                    $DB->exec("ALTER TABLE `pre_site` drop column `ktfz_domain`");
                    $DB->exec("ALTER TABLE `pre_site` change money rmb decimal(10,2) DEFAULT NULL");
                    $DB->exec("ALTER TABLE `pre_site` change point rmbtc decimal(10,2) DEFAULT NULL");
                    $DB->exec("ALTER TABLE `pre_site` change siteurl domain varchar(50) DEFAULT NULL");
                    $DB->exec("ALTER TABLE `pre_site` change siteurl2 domain2 varchar(50) DEFAULT NULL");
                    $DB->exec("ALTER TABLE `pre_site` change ktfz_siteurl ktfz_domain varchar(50) DEFAULT NULL");
                    $DB->exec("ALTER TABLE `pre_site` ADD INDEX domain (`domain`)");
                    $DB->exec("ALTER TABLE `pre_site` ADD INDEX domain2 (`domain2`)");
                    $DB->exec("ALTER TABLE `pre_site` ADD INDEX qq (`qq`)");
                    $DB->exec("ALTER TABLE `pre_site` ADD INDEX qq_openid (`qq_openid`)");
                    $DB->exec("ALTER TABLE `pre_site` ADD INDEX wx_openid (`wx_openid`)");
                    $DB->exec("ALTER TABLE `pre_site` ADD UNIQUE user (`user`)");

                }
                if (in_array('workorder', $permission)) {
                    $DB->exec("DROP TABLE IF EXISTS `pre_workorder`");
                    $DB->exec("ALTER TABLE `cmy_workorder` RENAME TO `pre_workorder`");
                    $DB->exec("ALTER TABLE `pre_workorder` change piclist `picurl` VARCHAR(150) DEFAULT NULL");
                    $DB->exec("ALTER TABLE `pre_workorder` ADD COLUMN `ts` tinyint(1) DEFAULT 0");
                }
                if (in_array('article', $permission)) {
                    $DB->exec("DROP TABLE IF EXISTS `pre_article`");
                    $DB->exec("ALTER TABLE `cmy_message` RENAME TO `pre_article`");
                }
                if (in_array('record', $permission)) {
                    $DB->exec("DROP TABLE IF EXISTS `pre_points`");
                    $DB->exec("ALTER TABLE `cmy_points` RENAME TO `pre_points`");
                    $DB->exec("ALTER TABLE `pre_workorder` ADD COLUMN `status` tinyint(1) NOT NULL DEFAULT '0'");
                    $DB->exec("ALTER TABLE `pre_site` ADD INDEX zid (`zid`)");
                    $DB->exec("ALTER TABLE `pre_site` ADD INDEX orderid (`orderid`)");

                }
                if (in_array('tixian', $permission)) {
                    $DB->exec("DROP TABLE IF EXISTS `pre_tixian`");
                    $DB->exec("ALTER TABLE `cmy_tixian` RENAME TO `pre_tixian`");
                    $DB->exec("ALTER TABLE `pre_tixian` ADD INDEX zid (`zid`)");
                    $DB->exec("ALTER TABLE `pre_tixian` ADD COLUMN `note` text DEFAULT NULL");

                }
                showmsg('沉梦数据迁移成功', 1);
            default:
                $permission = implode(',', $_POST['permission']);
                if (strpos($permission, 'order')) {
                    //$DB->exec("DELETE FROM pre_orders");
                    $DB->exec("ALTER TABLE `shua_orders` RENAME TO pre_orders`");
                }

                if (strpos($permission, 'tools')) {
                    //$DB->exec("DELETE FROM pre_tools");
                    $DB->exec("ALTER TABLE `shua_tools` RENAME TO pre_tools`");
                    $DB->exec("ALTER TABLE pre_tools` ADD `valiserv` VARCHAR(15) NULL AFTER `uptime`");
                    $DB->exec("ALTER TABLE pre_tools` ADD `sales` INT(11) NULL AFTER `validate`");
                    $DB->exec("ALTER TABLE pre_tools` ADD `stock` INT(11) NULL AFTER `sales`");
                    $DB->exec("ALTER TABLE pre_tools` ADD `addtime` datetime NULL AFTER `stock`");
                    $DB->exec("ALTER TABLE pre_tools` ADD COLUMN `goods_sid` tinyint(1) NOT NULL DEFAULT '0'");
                    $DB->exec("ALTER TABLE pre_tools` ADD COLUMN `audit_status` tinyint(1) NOT NULL DEFAULT '0'");
                    $DB->exec("ALTER TABLE pre_tools` ADD COLUMN `sup_price` decimal(10,2) NOT NULL DEFAULT '0.00'");
                    $DB->exec("ALTER TABLE pre_tools` ADD COLUMN `ts` tinyint(1) DEFAULT '0'");
                }
                if (strpos($permission, 'shequ')) {
                    //$DB->exec("DELETE FROM pre_shequ");
                    $DB->exec("ALTER TABLE `shua_shequ` RENAME TO pre_shequ`");
                    $DB->exec("ALTER TABLE pre_shequ` ADD `result` tinyint(1) NULL AFTER `type`");
                    $DB->exec("ALTER TABLE pre_tools` ADD `remark` VARCHAR(255) NULL AFTER `status`");
                    $DB->exec("ALTER TABLE pre_tools` ADD `protocol` tinyint(1) NULL AFTER `remark`");
                    $DB->exec("ALTER TABLE pre_tools` ADD `monitor` tinyint(1) NULL AFTER `protocol`");
                }
                if (strpos($permission, 'price')) {
                   // $DB->exec("DELETE FROM pre_price");
                    $DB->exec("ALTER TABLE `shua_price` RENAME TO pre_price`");
                }
                if (strpos($permission, 'faka')) {
                    //$DB->exec("DELETE FROM pre_faka");
                    $DB->exec("ALTER TABLE `shua_faka` RENAME TO pre_faka`");
                    $DB->exec("ALTER TABLE pre_faka` ADD COLUMN `ts` tinyint(1) DEFAULT '0'");
                }
                if (strpos($permission, 'site')) {
                    //$DB->exec("DELETE FROM pre_site");
                    $DB->exec("ALTER TABLE `shua_site` RENAME TO pre_site`");
                    $DB->exec("ALTER TABLE pre_site` ADD `email` tinyint(1) NULL AFTER `pwd`");
                    $DB->exec("ALTER TABLE pre_site` ADD `phone` VARCHAR(255) NULL AFTER `email`");
                    $DB->exec("ALTER TABLE pre_site` ADD `qq_openid` varchar(64) NULL AFTER `phone`");
                    $DB->exec("ALTER TABLE pre_site` ADD `wx_openid` varchar(64) NULL AFTER `qq_openid`");
                    $DB->exec("ALTER TABLE pre_site` ADD `nickname` varchar(64) NULL AFTER `wx_openid`");
                    $DB->exec("ALTER TABLE pre_site` ADD `faceimg` varchar(150) NULL AFTER `nickname`");
                    $DB->exec("ALTER TABLE pre_site` ADD `rmbtc` decimal(10,2) NULL AFTER `rmb`");
                    $DB->exec("ALTER TABLE pre_site` ADD `kfqq` varchar(12) NULL AFTER `description`");
                    $DB->exec("ALTER TABLE pre_site` ADD `kfwx` varchar(20) NULL AFTER `kfqq`");
                    $DB->exec("ALTER TABLE pre_site` ADD `appurl` varchar(150) NULL AFTER `iprice`");
                }
                if (strpos($permission, 'workorder')) {
                    //$DB->exec("DELETE FROM pre_workorder");
                    $DB->exec("ALTER TABLE `shua_workorder` RENAME TO pre_workorder`");
                    $DB->exec("ALTER TABLE `shua_workorder` ADD COLUMN `ts` tinyint(1) DEFAULT 0");
                }
                if (strpos($permission, 'message')) {
                    //$DB->exec("DELETE FROM pre_message");
                    $DB->exec("ALTER TABLE `shua_message` RENAME TO pre_message`");
                }
                if (strpos($permission, 'article')) {
                    //$DB->exec("DELETE FROM pre_article");
                    $DB->exec("ALTER TABLE `shua_article_list` RENAME TO pre_article`");
                    $DB->exec("ALTER TABLE pre_article` ADD `zid` INT(11) UNSIGNED NULL AFTER `id`");
                    $DB->exec("ALTER TABLE pre_article` RENAME COLUMN status TO active");
                    $DB->exec("ALTER TABLE pre_article` RENAME COLUMN seoKeywords TO keywords");
                    $DB->exec("ALTER TABLE pre_article` RENAME COLUMN seoDescription TO description");
                    $DB->exec("ALTER TABLE pre_article` ADD `color` varchar(20) NULL AFTER `description`");
                    $DB->exec("ALTER TABLE pre_article` RENAME COLUMN createTime TO addtime");
                    $DB->exec("ALTER TABLE pre_article` ADD `count` INT(11) NULL AFTER `addtime`");
                    $DB->exec("ALTER TABLE pre_article` ADD `top` tinyint(1) NULL AFTER `count`");
                }
                if (strpos($permission, 'record')) {
                    //$DB->exec("DELETE FROM pre_points");
                    $DB->exec("ALTER TABLE `shua_points` RENAME TO pre_points`");
                    $DB->exec("ALTER TABLE pre_points` ADD `status` tinyint(1) NULL AFTER `orderid`");
                }
                showmsg('祥云数据迁移成功', 1);
        }
    }
